import os
import requests
from PIL import Image


class pdf():
    def download(self, url):
        try:
            response = requests.get(url)
            response.encoding = "utf-8"
            json_dict = response.json()
            pageList = json_dict["data"]["htmlUrls"]["png"]
            title = json_dict["data"]["docId"]
            path = r"e:/"

            fileList = []
            for index, page in enumerate(pageList, start=1):
                resp = requests.get(page["pageLoadUrl"]).content
                file = f"{path}{index}.png"

                with open(file, "wb") as f:
                    f.write(resp)
                    fileList.append(file)
                    print(f"download:{index}")
            return True

        except Exception as e:
            #下载失败返回False
            return False

    def create_url(self, doc_id):
        url = "https://wenku.baidu.com/ndocview/readerinfo?doc_id=" + doc_id + "&docId=" + doc_id + "&type=html&clientType=100&pn=0&isFromBdSearch=0&srcRef=&rn=100&powerId=2&bizName=mainPc"
        return url

